package cn.fl.project.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.dto.SysOrgDTO;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.api.IPrjTransSettingApiService;
import cn.fl.project.condition.PrjTransSettingQueryCondition;
import cn.fl.project.dto.PrjTransSettingDTO;
import cn.fl.project.service.IPrjTransSettingService;
import cn.fl.project.transform.PrjTransSettingVOTransform;
import cn.fl.project.vo.PrjTransSettingVO;
import cn.fl.system.transform.SysOrgVOTransform;
import cn.fl.system.vo.SysOrgVO;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 项目转办组织架构设置表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class PrjTransSettingServiceImpl implements IPrjTransSettingService {

    /**
     * 日志
     */
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(PrjTransSettingServiceImpl.class);

    /**
     * 项目转办组织架构设置表 暴露服务
     */
    @Autowired
    private IPrjTransSettingApiService prjTransSettingApiService;
    /**
     * 数据字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

    @Override
    public PrjTransSettingVO selectById(Long id) throws BaseException {
        if (StringUtils.isEmpty(id)) return null;
        PrjTransSettingDTO dto = this.prjTransSettingApiService.selectPrjTransSettingById(id);
        return PrjTransSettingVOTransform.toVO(dto);
    }

    @Override
    public Long add(PrjTransSettingVO entity) throws BaseException {
        if (null == entity) return null;
        return this.prjTransSettingApiService.addPrjTransSetting(PrjTransSettingVOTransform.toDTO(entity));
    }

    @Override
    public boolean removeByIds(List<Long> ids) throws BaseException {
        if (CollectionUtils.isEmpty(ids)) return false;
        return this.prjTransSettingApiService.removePrjTransSettingByIds(ids, CurrentThreadContext.getCurrentUserId());
    }

    @Override
    public boolean modify(PrjTransSettingVO entity) throws BaseException {
        if (null == entity) return false;
        return this.prjTransSettingApiService.modifyPrjTransSetting(PrjTransSettingVOTransform.toDTO(entity));
    }

    @Override
    public Pager searchListPage(Pager page) throws BaseException {
        PrjTransSettingQueryCondition condition = null;
        if (null == page) {
            page = new Pager();
            page.setCondition(new PrjTransSettingQueryCondition());
        }
        condition = (PrjTransSettingQueryCondition) page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
        page = this.prjTransSettingApiService.searchPrjTransSettingListPage(page);
        if (CollectionUtils.isNotEmpty(page.getResultList())) {
            List<PrjTransSettingVO> voList = PrjTransSettingVOTransform.toVOList((List<PrjTransSettingDTO>) page.getResultList());
            //this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
    }

    @Override
    public List<PrjTransSettingVO> searchList(PrjTransSettingQueryCondition condition) throws BaseException {
        if (null == condition) condition = new PrjTransSettingQueryCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
        List<PrjTransSettingVO> voList = PrjTransSettingVOTransform.toVOList(this.prjTransSettingApiService.searchPrjTransSettingList(condition));
        this.setDictData(voList);
        return voList;
    }

    /**
     * 添加数据字典名称
     *
     * @param voList
     */
    public void setDictData(List<PrjTransSettingVO> voList) {
        if (CollectionUtils.isEmpty(voList)) return;
		/*Map<String, String> _CdMap = this.sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, PrjTransSettingConstant.CODE_CD);
		for(PrjTransSettingVO vo:voList){
			if (_CdMap != null && _CdMap.containsKey(vo.getCd())) {
            	vo.setCdNm(_CdMap.get(vo.getCd()));
        	}	
		}*/
    }

    /**
     * 查询数据字典
     *
     * @param groupCd 组代码
     * @return List<KeyValueVO>
     */
    public List<KeyValueVO> searchCdList(String groupCd) {
        return this.sysDictionaryApiService.getKvList(groupCd, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    /**
     * @param voList
     * @return
     */
    @Override
    public JSONArray sysOrgList2JsonArray(List<SysOrgVO> voList) throws BaseException {
        // 获取系统组织架构的dto
        List<SysOrgDTO> dtoList = SysOrgVOTransform.toDTOList(voList);
        JSONArray jsonArray = new JSONArray();
        if (CollectionUtils.isEmpty(dtoList)) {
            return jsonArray;
        }
        for (SysOrgDTO sysOrgDTO : dtoList) {
            JSONObject obj = new JSONObject();
            obj.put("ischecked", false);
            obj.put("id", sysOrgDTO.getId());
            obj.put("text", sysOrgDTO.getOrgNm());
            obj.put("pid", sysOrgDTO.getOrgPid());
            obj.put("orgCd", sysOrgDTO.getOrgCd());
            obj.put("orgDispNm", sysOrgDTO.getOrgDispNm());
            jsonArray.add(obj);
        }
        // JSONArray treeList = this.treeList(jsonArray, SysOrgConstant.SYS_ORG_CD_PID_INIT);
        return jsonArray;
    }

    /**
     * 递归添加整个集合数据
     *
     * @param treeList
     * @param parentId
     * @return
     */
    private JSONArray treeList(JSONArray treeList, Long parentId) throws BaseException {
        JSONArray childMenu = new JSONArray();
        for (Object object : treeList) {
            JSONObject jsonMenu = JSONObject.fromObject(object);
            long oid = jsonMenu.getLong("id");
            long pid = jsonMenu.getLong("pid");
            if (parentId == pid) {
                JSONArray c_node = treeList(treeList, oid);
                jsonMenu.put("children", c_node);
                childMenu.add(jsonMenu);
            }
        }
        return childMenu;
    }

    /**
     * 通过转出和转入组织架构id查询唯一的转办配置对象
     *
     * @param inOrgId
     * @param outOrgId
     * @return
     */
    @Override
    public PrjTransSettingVO searchPrjTransSettingVOByInOrgIdAndOutOrgId(Long inOrgId, Long outOrgId) throws BaseException {
        return PrjTransSettingVOTransform.toVO(prjTransSettingApiService.searchPrjTransSettingDTOByInOrgIdAndOutOrgId(inOrgId, outOrgId));
    }

    /**
     * 选择或取消转入机构后对IS_DEL进行修改
     *
     * @param prjTransSettingVO
     * @return
     */
    @Override
    public Long prjTransSettingUpdateIsDel(PrjTransSettingVO prjTransSettingVO) throws BaseException {
        return prjTransSettingApiService.prjTransSettingUpdateIsDel(PrjTransSettingVOTransform.toDTO(prjTransSettingVO));
    }

    /**
     * 根据当前选中的转出组织架构查询数据库全部曾经转办的集合
     *
     * @param qc
     * @return
     * @throws BaseException
     */
    @Override
    public List<PrjTransSettingVO> selectPrjTransSettingListByOutOrgId(PrjTransSettingQueryCondition qc) throws BaseException {
        return PrjTransSettingVOTransform.toVOList(prjTransSettingApiService.selectPrjTransSettingListByOutOrgId(qc));
    }
}
